/**
 * <p>文件名:		AnnotationTestBean.java</p>
 * <p>版权:		CopyrightTag</p>
 * <p>公司:		CompanyTag</p>
 * @author		周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 */

package com.caits.lbs.framework.services.jmx;

/**
 * <p>
 * AnnotationTestBean
 * </p>
 * <p>
 * 利用Spring的注解方便的实现jmx管理
 * 需要配置发现策略
 * <bean id="exporter"
 class="org.springframework.jmx.export.MBeanExporter">
 <property name="assembler" ref="assembler" />
 <property name="namingStrategy" ref="namingStrategy" />
 <property name="autodetect" value="true" />
 </bean>
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2010-12-10 下午05:10:41</td>
 *          </tr>
 *          <tr>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          </tr>
 *          </table>
 */
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.util.Assert;

import com.caits.lbs.framework.log.CommonLogFactory;

// 实例标记为由JMX管理的资源
@ManagedResource(objectName = "bean:name=logLevelJmx", description = "My Managed Bean", log = true, logFile = "jmx.log", currencyTimeLimit = 15, persistPolicy = "OnUpdate", persistPeriod = 200, persistLocation = "foo", persistName = "bar")
public class LogLevelAnnotation {

	/** 日志类 */
	private static Logger log = CommonLogFactory.getLog();

	/**
	 * 设置日志级别
	 * @param level
	 */
	private void setLevel(Level level) {
		log.setLevel(level);
	}

	/**
	 * 设置为debug
	 */
	@ManagedOperation(description = "setDebug")
	@ManagedOperationParameters({})
	public void setDebug() {
		Level level = Level.DEBUG;
		setLevel(level);
		log.debug("setDebug result.");
	}

	/**
	 * 设置为setInfo
	 */
	@ManagedOperation(description = "setInfo")
	@ManagedOperationParameters({})
	public void setInfo() {
		Level level = Level.INFO;
		setLevel(level);
		log.info("setInfo result.");
	}

	/**
	 * 设置为setWarn
	 */
	@ManagedOperation(description = "setWarn")
	@ManagedOperationParameters({})
	public void setWarn() {
		Level level = Level.WARN;
		setLevel(level);
		log.warn("setWarn result.");
	}

	/**
	 * 设置为setError
	 */
	@ManagedOperation(description = "setError")
	@ManagedOperationParameters({})
	public void setError() {
		Level level = Level.ERROR;
		setLevel(level);
		log.error("setError result.");
	}

	/**
	 * 设置为setFatal
	 */
	@ManagedOperation(description = "setFatal")
	@ManagedOperationParameters({})
	public void setFatal() {
		Level level = Level.FATAL;
		setLevel(level);
		log.fatal("setFatal result.");
	}

	/**
	 * 设置为setTrace
	 */
	@ManagedOperation(description = "setTrace")
	@ManagedOperationParameters({})
	public void setTrace() {
		Level level = Level.TRACE;
		setLevel(level);
		log.trace("setTrace result.");
	}

	/**
	 * 设置为setOff
	 */
	@ManagedOperation(description = "setOff")
	@ManagedOperationParameters({})
	public void setOff() {
		Level level = Level.OFF;
		setLevel(level);
		log.fatal("setOff result.");
	}

	public Level getLevel() {
		return log.getLevel();
	}

	public String getLevelString() {
		return log.getLevel().toString();
	}

	
	/**
	 * 两个数相加的JMX的操作  
	 * @param xxx	num1
	 * @param yyy	num2
	 * @return int	result
	 */
	@ManagedOperation(description = "Add two numbers")
	@ManagedOperationParameters({ @ManagedOperationParameter(name = "x", description = "The first number"),
	@ManagedOperationParameter(name = "y", description = "The second number") })
	public int add(final int xxx, final int yyy) {
		return xxx + yyy;
	}

	/**
	 * FIXME void
	 */
	public void dontExposeMe() {
		Assert.state(1 == 0, "dont call Me.");
	}

}
